/*
 * Copyright (c) 2013, Frank-Li.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This file is part of the Contiki OS
 *
 * $Id: contiki-main.c,v 1.2 2010/10/27 14:05:24 salvopitru Exp $
 */
/*---------------------------------------------------------------------------*/
/**
* \file
*			Contiki main file.
* \author
*			Frank Li <lgl88911@163.com>
*/
/*---------------------------------------------------------------------------*/

#include <stdio.h>         
#include "contiki.h"
#include "NUC123Series.h"
#include "CLOCK.h"

void UART02_IRQHandler(void)
{
	uint32_t u32uart0IntStatus;
	uint8_t u8InChar;

	if(UART_GET_RDA_INT(UART0) != 0)
	{
		/* Get all the input characters */
		while(UART_GET_RDA_IF(UART0) != 0)
		{
			/* Get the character from UART Buffer */
			u8InChar = UART_READ(UART0);
			///printf("read char %x\n\r", u8InChar);
			//printf("%c",u8InChar);
			SendChar(u8InChar);
			serial_line_input_byte(u8InChar);
		}
	}
}


/*---------------------------------------------------------------------------*/
int
main(void)
{
  
  /*
   * Initalize hardware.
   */
	STR_UART_T sParam;
	uint8_t u8Option;

	SYS_UnlockReg();                                        //En:Unlock protected registers
	GPIO_XT1();
	CLK_HXT_ENABLE();                                        //En:Enable External XTAL (4~24 MHz)
	while(!CLK_IS_STABLE(CLK_HXT_STB)); //En:Waiting for 12MHz clock ready
	SET_HCLK_CLKSRC(CLK_HCLK_HXT);                  //En:Switch HCLK clock source to XTAL
	SystemCoreClockUpdate();                                //En:Update the variable SystemCoreClock

	GPIO_UART0_RXTX();
	GPIO_EINT0();                        //En:Set PB multi-function pins for UART0 RXD, UART0 TXD and EINT0

	SYS_LockReg();                                          //En:Lock protected registers

	sParam.u32ClkSrc = CLK_UART_HXT;
	sParam.u32ClkDiv = 1;
	sParam.u32ClkFreq = __XTAL;
	sParam.u32BaudRate = 115200;
	sParam.u32cDataBits = UART_WORD_LEN_8;
	sParam.u32cStopBits = UART_STOP_BIT_1;
	sParam.u32cParity = UART_PARITY_NONE;
	sParam.u32cRxTriggerLevel = UART_FCR_RFITL_1BYTE;
	sParam.u32BaudMode = UART_BAUD_MODE2;
	DrvUART_Init (UART0, &sParam);
	DrvUART_EnableInt(UART0, UART_IER_RDA_IEN);

	printf("Hello world test\n\r");

	Sr04_init();
	DH11_Init();

  /*
   * Initialize Contiki and our processes.
   */
	clock_init();
	process_init();
	serial_line_init();
  
	rtimer_init();
  /* etimer_process should be initialized before ctimer */
  process_start(&etimer_process, NULL);  
  ctimer_init();
  netstack_init();

  procinit_init();

  autostart_start(autostart_processes);

  while(1) {
    
    int r;    
    
    do {
      /* Reset watchdog. */
      r = process_run();
    } while(r > 0);
  }
  
}

